
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// game name: THI TIM HIEU HA NOI VA THI TRUONG CHUNG KHOAN 
	/// channel: n/a
	/// command code: HN X Y
	/// created: 20/03/07 .
	/// Writer : Hungncv .
	/// </summary>
	/// 
	//--Tren bao ANTD-->///
	///AN_X_Y : 8359 (X: A,B,C,D : Chuong trinh HN ngan nam van hien)
	public class XONGDATXONGNHA
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();

		public XONGDATXONGNHA(SMS_MO MO)
		{
			WMO = MO;
		}

		public XONGDATXONGNHA()
		{
		}
		#endregion
		#region Method
		public void run()
		{
			WMT = new SMS_MT(WMO);

			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();           
			string[] msg = tmpMsg.Split(new char[] { ' ' });

			string strReturnMsg = "";            
			strReturnMsg = GetReturnMsg(msg);
			if (strReturnMsg.Length>160 && strReturnMsg.Length<=320)
			{
				string temp;
				temp = strReturnMsg;
				strReturnMsg = temp.Substring(0,160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				strReturnMsg = temp.Substring(160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			else
			{
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;  
		}

		private string GetReturnMsg(string[] arr)
		{
			string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
			string msgBody = "";
			string Help_XongDatXongNha = "";
			Help_XongDatXongNha = "Ban da nhan sai cu phap tin nhan!\n";
			Help_XongDatXongNha += "De xem thong tin Xong dat - Xong nha,\n";
			Help_XongDatXongNha += "Soan tin:\n";
			Help_XongDatXongNha += "XD <tuoi cua ban> gui 6772\n";
			Help_XongDatXongNha += "Vi du: Ban tuoi Dan, soan tin:\n";
			Help_XongDatXongNha += "XD DAN 6772";
			if ((WMO.ServiceID.Replace("04","") == "6772") || (WMO.UserID=="841278882664"))
			{
				string strSQL="";
				strSQL ="SELECT Description FROM XONGDAT_XONGNHA\n";
				strSQL += "WHERE Age='" + arr[0] + "'";
				SqlConnection sqlconn = new SqlConnection(ConfigSettings.ReadSetting("CONNECTION_STRING_7x62"));
				sqlconn.Open();
				SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlconn);
				DataSet ds = new DataSet();
				da.Fill(ds);
				if (ds.Tables[0].Rows.Count==0)
				{
					msgBody = Help_XongDatXongNha;
				}
				else
				{
					msgBody = ds.Tables[0].Rows[0]["Description"].ToString();
				}
                ds.Dispose();
                sqlconn.Close();
                sqlconn.Dispose();
			}
			else
			{
				msgBody = Help_XongDatXongNha;
			}
			return msgBody;
		}
			#endregion Method
	}
}
